package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.service.FinanFlowQueryService;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.AdditionalFlowInvestorDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowProjectDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowRepayPlanDao;
import com.qdlc.p2p.dal.dao.AdditionalFlowYieldPlanDao;
import com.qdlc.p2p.dal.dto.AdditionalFlowInvestor;
import com.qdlc.p2p.dal.dto.AdditionalFlowProject;
import com.qdlc.p2p.dal.dto.AdditionalFlowRepayPlan;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.FlowInvestorModel;
import com.qdlc.p2p.dal.model.FlowProjectModel;
import com.qdlc.p2p.dal.model.FlowYieldPlanModel;
import com.qdlc.p2p.dal.model.RepayPlanModel;

@Repository("finanFlowQueryService")
@Transactional
public class FinanFlowQueryServiceImpl implements FinanFlowQueryService {

    @Resource
    private AdditionalFlowProjectDao additionalFlowProjectDao;

    @Resource
    private AdditionalFlowInvestorDao additionalFlowInvestorDao;

    @Resource
    private AdditionalFlowYieldPlanDao additionalFlowYieldPlanDao;

    @Resource
    private AdditionalFlowRepayPlanDao additionalFlowRepayPlanDao;

    @Override
    public PageDataList<FlowProjectModel> getPageFlowProject(
            FlowProjectModel projectModel) {
        QueryParam param = QueryParam.getInstance().addOrder(OrderType.DESC, "id");
        byte status = projectModel.getQueryStatus();
        if (status != 99) {
            param.addParam("status", projectModel.getQueryStatus());
        }
        if (!StringUtil.isBlank(projectModel.getSearchName())) {
            param.addParam("user.userName", Operators.LIKE, projectModel.getSearchName());
        }
        param.addPage(projectModel.getPage(), projectModel.getRows());
        PageDataList<AdditionalFlowProject> list = additionalFlowProjectDao.findPageList(param);
        PageDataList<FlowProjectModel> pageModelList = new PageDataList<FlowProjectModel>();
        List<FlowProjectModel> modelList = new ArrayList<FlowProjectModel>();
        for (AdditionalFlowProject project : list.getList()) {
            FlowProjectModel model = FlowProjectModel.instance(project);
            model.setUserName(project.getUser().getUserName());
            model.setUserId(project.getUser().getUserId());
            model.setUser(null);
            modelList.add(model);
        }
        pageModelList.setList(modelList);
        pageModelList.setPage(param.getPage());
        return pageModelList;
    }

    @Override
    public AdditionalFlowProject getFlowProjectById(long id) {
        return additionalFlowProjectDao.find(id);
    }

    @Override
    public List<AdditionalFlowInvestor> getFlowInvestor(long pid) {
        List<AdditionalFlowInvestor> list = additionalFlowInvestorDao.getFlowInvestor(pid);
        return list;
    }

    @Override
    public PageDataList<FlowInvestorModel> getInvestorList(long pid, int page, int size) {
        return additionalFlowInvestorDao.getFlowInvestorModel(pid, page, size);
    }

    @Override
    public PageDataList<FlowInvestorModel> getUserInvestList(long userId, FlowProjectModel model) {
        return additionalFlowInvestorDao.getUserTenderInvest(userId, model);
    }

    @Override
    public PageDataList<FlowYieldPlanModel> getUserYieldPlan(long userId,
                                                             FlowProjectModel model) {
        return additionalFlowYieldPlanDao.getYieldPlanList(userId, model);
    }

    @Override
    public List<AdditionalFlowRepayPlan> getFlowRepayPlanList(long projectId) {
        List<AdditionalFlowRepayPlan> list = additionalFlowRepayPlanDao.getRepayByPid(projectId);
        return list;
    }

    @Override
    public List<FlowProjectModel> getFlowProjectIndex() {
        return additionalFlowProjectDao.getIndexFlow();
    }

    @Override
    public AdditionalFlowProject getProjcetByUuid(String uuid) {
        return additionalFlowProjectDao.getFlowProjectByUuid(uuid);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public PageDataList<FlowProjectModel> getBorrowFlowProject(
            FlowProjectModel model) {

        QueryParam param = QueryParam.getInstance();
        byte status = model.getQueryStatus();
        if (status != 99) {
            param.addParam("status", model.getQueryStatus());
        }
        param.addParam("user.userId", model.getUser().getUserId());
        param.addPage(model.getPage(), model.getRows());
        PageDataList<AdditionalFlowProject> list = additionalFlowProjectDao.findPageList(param);
        PageDataList<FlowProjectModel> pageModelList = new PageDataList<FlowProjectModel>();
        List<FlowProjectModel> modelList = new ArrayList<FlowProjectModel>();
        for (AdditionalFlowProject project : list.getList()) {
            FlowProjectModel pmodel = FlowProjectModel.instance(project);
            pmodel.setUserName(project.getUser().getUserName());
            List relist = additionalFlowRepayPlanDao.getRepayByPid(project.getId());
            if (null != list && relist.size() > 0) {
                pmodel.setIsRepayTag(1);
            }

            modelList.add(pmodel);
        }
        pageModelList.setList(modelList);
        pageModelList.setPage(param.getPage());
        return pageModelList;
    }

    @Override
    public PageDataList<RepayPlanModel> getRepayPlanPage(FlowProjectModel model) {
        QueryParam param = QueryParam.getInstance();
        byte status = model.getQueryStatus();
        if (status != 99) {
            param.addParam("status", model.getQueryStatus());
        }
        param.addParam("flowProject.id", model.getId());
        param.addParam("user.userId", model.getUser().getUserId());
        param.addPage(model.getPage(), model.getRows());
        PageDataList<AdditionalFlowRepayPlan> repayPlanPageList = additionalFlowRepayPlanDao.findPageList(param);
        PageDataList<RepayPlanModel> reModelPage = new PageDataList<RepayPlanModel>();
        List<RepayPlanModel> modelList = new ArrayList<RepayPlanModel>();
        for (AdditionalFlowRepayPlan repay : repayPlanPageList.getList()) {
            RepayPlanModel rmodel = RepayPlanModel.instance(repay);
            rmodel.setProjectName(repay.getFlowProject().getName());
            rmodel.setUuid(repay.getFlowProject().getUuid());
            double repayAccount = BigDecimalUtil.add(repay.getRepayCopies(), repay.getRepayMoney());
            rmodel.setRepaySumAccount(repayAccount);
            rmodel.setFlowProject(null);
            rmodel.setUser(null);
            if (null == repay.getRepayYestime() && repay.getStatus() == 0) {
                rmodel.setDateStr("--");
            } else {
                rmodel.setDateStr(DateUtil.dateStr2(repay.getRepayTime()));
            }

            modelList.add(rmodel);
        }
        reModelPage.setList(modelList);
        reModelPage.setPage(param.getPage());
        return reModelPage;
    }

    @Override
    public PageDataList<FlowProjectModel> getindexFlowList(
            FlowProjectModel model) {
        return additionalFlowProjectDao.getIndexFlowList(model);
    }

    @Override
    public List<Object[]> getFlowCapitalByUserAndDate(User user, String date) {
        return additionalFlowYieldPlanDao.getFlowCapitalByUserAndDate(user, date);
    }

    @Override
    public List<Object[]> getFlowInterestByUserAndDate(User user, String date) {
        return additionalFlowYieldPlanDao.getFlowInterestByUserAndDate(user, date);
    }
}
